﻿

@using SubstrateHelper.Models;
@using SubstrateHelper.Data;
@inject SubstrateHelperDbContext Db
@inject MessageService MessageService



<div>
    <div class="m-1">
        <span  class="text-success">
            Select * from <strong class="text-danger">SubstrateInfos </strong> where
            <input type="text" style="width:300px" @bind="StringWhere" class="text-success" /> 
            <strong> ;</strong>
        </span>
        <span class="ml-3">
            数据列辅助输入：
            <select @bind="SelectedField">
                <option value="-1">无</option>
                @if (Fields != null)
                    for (int i = 0; i < Fields.Count; i++)
                    {
                        <option value="@i">@Fields[i].Value</option>
                    }
                }

            </select>
        </span>
        <Button class="ml-5" IsOutline="false" Color="Color.Success" @onclick="DoQuery">查  询</Button>

        <span class="ml-5">
            注：高级查询需要一些简单的SQL (Structured Query Language 结构化查询语言) 知识。
            <a href="https://www.runoob.com/sql/sql-where.html" target="_blank">访问SQL教程</a>
        </span>

    </div>

    <hr class="bg-primary" style="height:5px;" />
    <div>
        <ComponentSubstratesQueryResult Items="@SubstrateInfos" />
    </div>


</div>
<Message />


@code {



    List<SubstrateInfo> SubstrateInfos { get; set; }

    List<KeyValuePair<string, string>> Fields
    {
        get
        {
            if (_Fields == null)
            {
                Type type = typeof(SubstrateInfo);
                var props=type.GetProperties();
                Type typeDisplayName = typeof(System.ComponentModel.DisplayNameAttribute);
                _Fields = new List<KeyValuePair<string, string>>();
                foreach(var prop in props)
                {
                    var att=Attribute.GetCustomAttribute(prop, typeDisplayName) as System.ComponentModel.DisplayNameAttribute;
                    if (att != null)
                    {
                        _Fields.Add(new KeyValuePair<string, string>(prop.Name, att.DisplayName));
                    }
                }
                _Fields = _Fields.OrderBy(x => x.Value).ToList();
            }
            return _Fields;
        }
    }
    List<KeyValuePair<string, string>> _Fields = null;
    int SelectedField
    {
        get
        {
            return _SelectedField;
        }
        set
        {
            _SelectedField = value;
            if (_SelectedField >= 0)
            {
                StringWhere += Fields[_SelectedField].Key;
            }
        }
    }
    int _SelectedField = -1;


    string StringWhere { get; set; } = "SchedulingDate is null";

    private void DoQuery()
    {

        try
        {
            var trim = StringWhere.Trim().Replace(";", "");
            var sql = $"select * from SubstrateInfos where {trim};";

            var a = Db.Database.SqlQuery(sql);
            SubstrateInfos = a.TableToList<SubstrateInfo>();

            MessageService?.Show(new MessageOption()
            {
                Content = $"返回{SubstrateInfos.Count}条结果。",
                Icon = "fa fa-info-circle",
                ShowDismiss = true,
                Color = Color.Success
            });

        }
        catch (Exception ex)
        {
            MessageService?.Show(new MessageOption()
            {
                Content = $"发生错误：{ex.Message}",
                Icon = "fa fa-info-circle",
                ShowDismiss = true,
                Color = Color.Danger
            });
        }

    }


}
